﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");

		termId = event.getArg("TermID");

		/* 学籍相关信息 */
		studentId = sessionAdvice.getAutherUserID();
		
		/* 读取学生已选课程的排课记录 */
		sql = "SELECT 
				t_course.course_name, 
				tasktime, 
				case when taskclsroom is null then '-' else taskclsroom end taskclsroom, 
				task_week, 
				t_task_arrange.task_type, t_task_arrange.task_id 
				FROM t_task_timetable 
				INNER JOIN t_task_arrange ON t_task_arrange.task_id = t_task_timetable.task_id 
				INNER JOIN t_course ON t_course.cid = t_task_arrange.cid 
				WHERE 
					EXISTS (
						SELECT 1 FROM t_student_course 
						WHERE 
							t_task_arrange.task_id = t_student_course.task_id 
							AND 
							t_task_arrange.term_id = :termId 
							AND 
							t_student_course.stu_id = :studentId
					) ";
		
		queryObj = new Query( datasource=application.dnsSlave );
		queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );

		rs_table = queryObj.execute( sql=sql ).getResult();

		
		timeTable = arrayNew(2);
		
		/* 课表冲突标记 */
		for ( lec=1; lec lte 6; lec++ ) {
			for ( wkday=1; wkday lte 7; wkday++ ) {
				timeTable[wkday][lec] = 0;
			}
		}
		
		
		timeTableSet = structNew();
		
		/* 生成课表显示数据 */
		
		for( a=1; a LTE rs_table.recordCount; a++ ){
			
			currentTaskTime = rs_table["tasktime"][a];
			
			if ( rs_table["taskclsroom"][a] eq "-" ) { 
				currentTaskPlace = "";
			}
			else {
				currentTaskPlace = rs_table["taskclsroom"][a];
			}
			
			wkday = listGetAt(currentTaskTime, 1, "-@");
			lec = listGetAt(currentTaskTime, 2, "-@");
			cell = listGetAt(currentTaskTime, 3, "-@");
			
			/* 生成任课教师 */
			switch ( rs_table["task_type"][a] ) {
				case "P":
					sql = "SELECT 
							p.tch_name teacher_pri, s.tch_name teacher_sec, a.tch_name teacher_ace 
							FROM t_task t 
							INNER JOIN t_teacher p ON p.tch_id = t.tch_id 
							INNER JOIN t_teacher s ON s.tch_id = t.tch_id_sec 
							INNER JOIN t_teacher a ON a.tch_id = t.tch_id_ace 
							WHERE tsk_id = :taskId";
					break;
					
				case "C":
					sql = "SELECT 
							p.tch_name teacher_pri, s.tch_name teacher_sec, a.tch_name teacher_ace 
							FROM t_common_task t
							INNER JOIN t_teacher p ON p.tch_id = t.tch_id 
							INNER JOIN t_teacher s ON s.tch_id = t.tch_id_sec 
							INNER JOIN t_teacher a ON a.tch_id = t.tch_id_ace 
							WHERE tsk_id = :taskId";
					break;
					
				case "T":
					sql = "SELECT 
							p.tch_name teacher_pri, s.tch_name teacher_sec, a.tch_name teacher_ace 
							FROM t_sport_task t
							INNER JOIN t_teacher p ON p.tch_id = t.tch_id 
							INNER JOIN t_teacher s ON s.tch_id = t.tch_id_sec 
							INNER JOIN t_teacher a ON a.tch_id = t.tch_id_ace 
							WHERE tsk_id = :taskId";
					break;
					
				case "R":
					sql = "SELECT 
							p.tch_name teacher_pri, s.tch_name teacher_sec, a.tch_name teacher_ace 
							FROM t_reopen_task t
							INNER JOIN t_teacher p ON p.tch_id = t.tch_id 
							INNER JOIN t_teacher s ON s.tch_id = t.tch_id_sec 
							INNER JOIN t_teacher a ON a.tch_id = t.tch_id_ace 
							WHERE tsk_id = :taskId";
					break;
			}
			
			queryObj = new Query( datasource=application.dnsSlave );
			queryObj.addParam( name="taskId", value=rs_table["task_id"][a], cfsqltype="cf_sql_varchar" );

			rs_teacher = queryObj.execute( sql=sql ).getResult();

			currentTeacher = rs_teacher.teacher_pri;
			
			if ( not listFind( currentTeacher, rs_teacher.teacher_sec, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_teacher.teacher_sec;
			}
			
			if ( not listFind( currentTeacher, rs_teacher.teacher_ace, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_teacher.teacher_ace;
			}
			
			/* 
				生成课表内容 
				cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
			*/
			if ( cell eq 2 ) {

				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, cell, rs_table["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			if ( cell eq 3 and lec eq 2 ) {

				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, cell, rs_table["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			if ( cell eq 3 and lec eq 5 ) {

				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_table["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );

				}
				
				/* 晚上3节延续1学时 */
				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, 1, rs_table["task_week"][a] ];
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			/* 4学时延续 */
			if ( cell eq 4 ) {
				
				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_table["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			}
			
			/* 5学时延续 */
			if ( cell eq 5 ) {
				
				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_table["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, 3, rs_table["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			}
			
			/* 8学时延续 */
			if ( cell eq 8 ) {
				
				cellCourse = [ rs_table["course_name"][a], currentTeacher, currentTaskPlace, 2, rs_table["task_week"][a] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+2;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+2] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+3;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+3] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

			}

		}
		
	</cfscript>
</cfsilent>

<table class="UICourseTable">
	<thead>
		<tr>
			<td colspan="2" width="2%"></td>
			<td width="14%">星期一</td>
			<td width="14%">星期二</td>
			<td width="14%">星期三</td>
			<td width="14%">星期四</td>
			<td width="14%">星期五</td>
			<td width="14%">星期六</td>
			<td width="14%">星期天</td>
		</tr>
	</thead>
	<tbody>
		<cfloop from="1" to="6" index="lec">
			<tr>
				<cfswitch expression="#lec#">
					<cfcase value="1">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="3">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="5">
						<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
				</cfswitch>
				<td><cfoutput>#mathAdvice.convertLectureIdxToChinese(lec)#</cfoutput></td>
				<cfloop from="1" to="7" index="wkday">
					<cfset lectureKey = "KB" & wkday & lec />
					<td <cfif timeTable[wkday][lec]>class="attention"</cfif>>
						<cfif structKeyExists( timeTableSet, lectureKey )>
							<cfset i = 0 />
							<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
								<cfset i++ />
								<div class="lecture">
									<span class="course"><cfoutput>#cellCourse[1]#</cfoutput></span> - <span class="teacher"><cfoutput>#cellCourse[2]#</cfoutput></span>
									<br/>
									<span class="week"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
									<cfif cellCourse[3] neq "-">
										<br/>
										<span class="place"><cfoutput>#cellCourse[3]#</cfoutput></span>
									</cfif>
								</div>
								<cfif i lt arrayLen(timeTableSet[lectureKey]) >
									<div class="cellBreaker"><!--.--></div>
								</cfif>
							</cfloop>
						</cfif>
					</td>
				</cfloop>
			</tr>
		</cfloop>
	</tbody>
</table>